
package com.sunwayhorizo.health.hl7.factories;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.CE;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.SET_II;
import com.sunwayhorizo.health.hl7.persistent.InfrastructureRoot;
import com.sunwayhorizo.health.hl7.rim.Container;
import com.sunwayhorizo.health.hl7.rim.Device;
import com.sunwayhorizo.health.hl7.rim.Hl7Entity;
import com.sunwayhorizo.health.hl7.rim.LanguageCommunication;
import com.sunwayhorizo.health.hl7.rim.LivingSubject;
import com.sunwayhorizo.health.hl7.rim.ManufacturedMaterial;
import com.sunwayhorizo.health.hl7.rim.Material;
import com.sunwayhorizo.health.hl7.rim.NonPersonLivingSubject;
import com.sunwayhorizo.health.hl7.rim.Organization;
import com.sunwayhorizo.health.hl7.rim.Person;
import com.sunwayhorizo.health.hl7.rim.Place;
import com.sunwayhorizo.health.hl7.vocabulary.EntityClass;

public class EntityFactoryImpl extends EntityFactory
{

    public static final String RCS_ID = "$Header: EntityFactoryImpl.java 1.0 2006/12/08 09:16:40 amchanda noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: EntityFactoryImpl.java 1.0 2006/12/08 09:16:40 amchanda noship $", "com.sunwayhorizo.health.facade.factories.comm");
//    private RimServiceBean session;
    private FactoryValidation validation;
    private DataTypeFactory dataTypeFactory;
    
    public EntityFactoryImpl() {    
    }

//    public EntityFactoryImpl(ServiceLocator servicelocator)
//        throws Hl7Exception
//    {
////        session = (RimServiceBean)servicelocator.getRimService();
//        dataTypeFactory = DataTypeFactory.getInstance(servicelocator);
////        validation = new FactoryValidation(session);
//    }

    public Hl7Entity newHl7Entity(CS cs, CE ce, CS cs1, SET_II set_ii)
        throws Hl7Exception
    {
        Object obj = null;
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("entityClass");
        if(cs1 == null || cs1.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        if(cs.equal(EntityClass.STATE).isTrue())
            obj = newState(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.LIV).isTrue())
            obj = newLivingSubject(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.CONT).isTrue())
            obj = newContainer(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.CITY).isTrue())
            obj = newCity(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.GROUP).isTrue())
            obj = newGroup(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.PSN).isTrue())
            obj = newPerson(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.PLC).isTrue())
            obj = newPlace(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.CER).isTrue())
            obj = newCertificateRepresentation(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.NAT).isTrue())
            obj = newNation(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.CHEM).isTrue())
            obj = newChemicalSubstance(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.PUB).isTrue())
            obj = newPublicInstitution(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.ORG).isTrue())
            obj = newOrganization(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.MIC).isTrue())
            obj = newMicroorganism(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.ANM).isTrue())
            obj = newAnimal(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.NLIV).isTrue())
            obj = newNonPersonLivingSubject(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.MODDV).isTrue())
            obj = newImagingModality(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.PLNT).isTrue())
            obj = newPlant(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.HCE).isTrue())
            obj = newHealthChart(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.HOLD).isTrue())
            obj = newHolder(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.FOOD).isTrue())
            obj = newFood(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.MMAT).isTrue())
            obj = newManufacturedMaterial(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.PROVINCE).isTrue())
            obj = newProvince(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.MAT).isTrue())
            obj = newMaterial(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.COUNTRY).isTrue())
            obj = newCountry(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.DEV).isTrue())
            obj = newDevice(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.COUNTY).isTrue())
            obj = newCounty(ce, cs1, set_ii);
        else
        if(cs.equal(EntityClass.RGRP).isTrue())
        {
            obj = newResourceGroup(ce, cs1, set_ii);
        } else
        {
            obj = new Hl7Entity();
            
/*            if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
            {
                ((Hl7Entity)obj).setId( dataTypeFactory.newSET_II(session.getNextII()));
            } else
            {
                validation.validateOID(set_ii);
                ((Hl7Entity)obj).setId( set_ii.union(session.getNextII()));
            }
            ((Hl7Entity)obj).setCode( ce);
            ((Hl7Entity)obj).setDeterminerCode( cs1);
*/
        }

        return ((Hl7Entity) (obj));
    }

    public Hl7Entity newHealthChart(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Hl7Entity entity = new Hl7Entity();
        entity.setCode( ce);
        entity.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            entity.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            entity.setId( set_ii.union(session.getNextII()));
        }
        */
        entity.setClassCode( EntityClass.HCE);
        return entity;
    }

    /**
     * 构建Hl7Entity
     * @param ce
     * @param cs
     * @param set_ii
     * @return
     * @throws Hl7Exception  Hl7Entity
     */
    public Hl7Entity newHl7Entity(CE ce, CS cs, SET_II set_ii) throws Hl7Exception{
    	 Hl7Entity entity = new Hl7Entity();
    	 entity.setCode(ce);
    	 entity.setDeterminerCode(cs);
    	 entity.setClassCode(EntityClass.ENT);
//    	 entity.setId(set_ii);
    	 return entity;
    }
    
    public Organization newOrganization(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Organization organization = new Organization();
        organization.setCode( ce);
        organization.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            organization.setId( set_ii.union(session.getNextII()));
        }
 */       
        organization.setClassCode(EntityClass.ORG);
        
        return organization;
    }
    /* 
    public Organization newDepartment(CE ce, CS cs, SET_II set_ii)
    	throws Hl7Exception
    {
	    if(cs == null || cs.isNull().isTrue())
	        throw validation.newParameterMissingException("determiner");
	    Organization organization = new Organization();
	    organization.setCode( ce);
	    organization.setDeterminerCode( cs);
	       if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
	    {
	        organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
	    } else
	    {
	        validation.validateOID(set_ii);
	        organization.setId( set_ii.union(session.getNextII()));
	    }
	      
	    organization.setClassCode(EntityClass.DEPARTMENTORG);
	    
	    return organization;
    }*/ 
    
    /**
     * 单位
     * @param ce
     * @param cs
     * @param set_ii
     * @return
     * @throws Hl7Exception
     */
    /*
    public Organization newHospitalOrg(CE ce, CS cs, SET_II set_ii)
    	throws Hl7Exception
    {
	    if(cs == null || cs.isNull().isTrue())
	        throw validation.newParameterMissingException("determiner");
	    Organization organization = new Organization();
	    organization.setCode( ce);
	    organization.setDeterminerCode( cs);
	        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
	    {
	        organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
	    } else
	    {
	        validation.validateOID(set_ii);
	        organization.setId( set_ii.union(session.getNextII()));
	    }
	      
	    organization.setClassCode(EntityClass.HOSPITALORG);
	    
	    return organization;
    }*/ 
    
    /* 
    public  Organization newWorkingGroup(CE ce, CS cs, SET_II set_ii)throws Hl7Exception {
    	if(cs == null || cs.isNull().isTrue())
	        throw validation.newParameterMissingException("determiner");
	    Organization organization = new Organization();
	    organization.setCode( ce);
	    organization.setDeterminerCode( cs);
	       if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
	    {
	        organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
	    } else
	    {
	        validation.validateOID(set_ii);
	        organization.setId( set_ii.union(session.getNextII()));
	    }
	       
	    organization.setClassCode(EntityClass.WORKINGGROUPORG);
	    return organization;
    }*/

    public Organization newPublicInstitution(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Organization organization = new Organization();
        organization.setCode( ce);
        organization.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            organization.setId( set_ii.union(session.getNextII()));
        }
*/
        organization.setClassCode( EntityClass.PUB);
        return organization;
    }

    public Organization newNation(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Organization organization = new Organization();
        organization.setCode( ce);
        organization.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            organization.setId( set_ii.union(session.getNextII()));
        }
*/
        organization.setClassCode( EntityClass.NAT);
        return organization;
    }

    public Organization newState(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Organization organization = new Organization();
        organization.setCode( ce);
        organization.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            organization.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            organization.setId( set_ii.union(session.getNextII()));
        }
        */
        organization.setClassCode( EntityClass.STATE);
        return organization;
    }


    public Person newPerson(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Person person = new Person();
        person.setCode(ce);
        person.setDeterminerCode(cs);   
        
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            person.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            person.setId( set_ii.union(session.getNextII()));
        }
*/
        person.setClassCode(EntityClass.PSN);         
       // person.setId(set_ii);                             
        return person;
    }
    
    /**
     * 
    	* @Title: newPerson
    	* @Description: newPerson
    	* @param @param ce person cd 如患者ID等
    	* @param @param cs 
    	* @param @return
    	* @return Person    返回类型
    	* @throws Hl7Exception
    	* @firstAuthor jinzw
    	* @firstTime   2010-11-15 上午11:42:26
    	* @lastAuthor  jinzw
    	* @lastTime    2010-11-15 上午11:42:26
     */
	public Person newPerson(CE ce, CS cs) throws Hl7Exception {
		if (cs == null || cs.isNull().isTrue())
			throw validation.newParameterMissingException("determiner");
		Person person = new Person();
		person.setCode(ce);
		person.setDeterminerCode(cs);
		person.setClassCode(EntityClass.PSN);
		return person;
	}


    public LivingSubject newLivingSubject(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        LivingSubject livingsubject = new LivingSubject();
        livingsubject.setCode( ce);
        livingsubject.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            livingsubject.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            livingsubject.setId( set_ii.union(session.getNextII()));
        }
*/        
        livingsubject.setClassCode(EntityClass.LIV);
        return livingsubject;
    }

    public NonPersonLivingSubject newNonPersonLivingSubject(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        NonPersonLivingSubject nonpersonlivingsubject = new NonPersonLivingSubject();
        nonpersonlivingsubject.setCode( ce);
        nonpersonlivingsubject.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            nonpersonlivingsubject.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            nonpersonlivingsubject.setId( set_ii.union(session.getNextII()));
        }
*/        
        nonpersonlivingsubject.setClassCode(EntityClass.NLIV);
        return nonpersonlivingsubject;
    }

    public NonPersonLivingSubject newAnimal(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        NonPersonLivingSubject nonpersonlivingsubject = new NonPersonLivingSubject();
        nonpersonlivingsubject.setCode( ce);
        nonpersonlivingsubject.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            nonpersonlivingsubject.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            nonpersonlivingsubject.setId( set_ii.union(session.getNextII()));
        }
        */
        nonpersonlivingsubject.setClassCode( EntityClass.ANM);
       
        return nonpersonlivingsubject;
    }

    public NonPersonLivingSubject newMicroorganism(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        NonPersonLivingSubject nonpersonlivingsubject = new NonPersonLivingSubject();
        nonpersonlivingsubject.setCode( ce);
        nonpersonlivingsubject.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            nonpersonlivingsubject.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            nonpersonlivingsubject.setId( set_ii.union(session.getNextII()));
        }
        */
        nonpersonlivingsubject.setClassCode( EntityClass.MIC);
        return nonpersonlivingsubject;
    }

    public NonPersonLivingSubject newPlant(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        NonPersonLivingSubject nonpersonlivingsubject = new NonPersonLivingSubject();
        nonpersonlivingsubject.setCode( ce);
        nonpersonlivingsubject.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            nonpersonlivingsubject.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            nonpersonlivingsubject.setId( set_ii.union(session.getNextII()));
        }
        */
        nonpersonlivingsubject.setClassCode( EntityClass.PLNT);
        return nonpersonlivingsubject;
    }

    public Material newMaterial(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Material material = new Material();
        material.setCode( ce);
        material.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            material.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            material.setId( set_ii.union(session.getNextII()));
        }
*/        
        material.setClassCode(EntityClass.MAT);
        return material;
    }

    public Material newChemicalSubstance(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Material material = new Material();
        material.setCode( ce);
        material.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            material.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            material.setId( set_ii.union(session.getNextII()));
        }
        */
        material.setClassCode( EntityClass.CHEM);
        return material;
    }

    public Material newFood(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Material material = new Material();
        material.setCode( ce);
        material.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            material.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            material.setId( set_ii.union(session.getNextII()));
        }
        */
        material.setClassCode( EntityClass.FOOD);
        return material;
    }

    public ManufacturedMaterial newManufacturedMaterial(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        ManufacturedMaterial manufacturedmaterial = new ManufacturedMaterial();
        manufacturedmaterial.setCode( ce);
        manufacturedmaterial.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            manufacturedmaterial.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            manufacturedmaterial.setId( set_ii.union(session.getNextII()));
        }
*/
        manufacturedmaterial.setClassCode(EntityClass.MMAT);
        return manufacturedmaterial;
    }

    public Container newContainer(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Container container = new Container();
        container.setCode(ce);
        container.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            container.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            container.setId( set_ii.union(session.getNextII()));
        }
*/        
        container.setClassCode(EntityClass.CONT);
        return container;
    }

    public Container newHolder(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Container container = new Container();
        container.setCode( ce);
        container.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            container.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            container.setId( set_ii.union(session.getNextII()));
        }
        */
        container.setClassCode( EntityClass.HOLD);
        return container;
    }

    public Device newDevice(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Device device = new Device();
        device.setCode( ce);
        device.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            device.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            device.setId( set_ii.union(session.getNextII()));
        }
*/        
        device.setClassCode(EntityClass.DEV);
        return device;
    }

    public Device newCertificateRepresentation(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Device device = new Device();
        device.setCode( ce);
        device.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            device.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            device.setId( set_ii.union(session.getNextII()));
        }
        */
        device.setClassCode( EntityClass.CER);
        return device;
    }

    public Device newImagingModality(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Device device = new Device();
        device.setCode( ce);
        device.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            device.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            device.setId( set_ii.union(session.getNextII()));
        }
        */
        device.setClassCode( EntityClass.MODDV);
        return device;
    }

    public Place newPlace(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
*/        
        place.setClassCode(EntityClass.PLC);
        return place;
    }
    
    public Place newBed(CE ce, CS cs, SET_II set_ii)
    	throws Hl7Exception
	{
	    if(cs == null || cs.isNull().isTrue())
	        throw validation.newParameterMissingException("determiner");
	    Place place = new Place();
	    place.setCode( ce);
	    place.setDeterminerCode( cs);
	/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
	    {
	        place.setId( dataTypeFactory.newSET_II(session.getNextII()));
	    } else
	    {
	        validation.validateOID(set_ii);
	        place.setId( set_ii.union(session.getNextII()));
	    }
	*/        
	    place.setClassCode(EntityClass.PLC);
	    return place;
	}

    public Place newCity(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
        */
        place.setClassCode( EntityClass.CITY);
        return place;
    }
    
    /*
    public Place newRoom(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
                if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
        
        place.setClassCode( EntityClass.ROOM);
        return place;
    }*/

    public Place newCountry(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
        */
        place.setClassCode( EntityClass.COUNTRY);
        return place;
    }

    public Place newCounty(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
        */
        place.setClassCode( EntityClass.COUNTY);
        return place;
    }

    public Place newProvince(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Place place = new Place();
        place.setCode( ce);
        place.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            place.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            place.setId( set_ii.union(session.getNextII()));
        }
        */
        place.setClassCode( EntityClass.PROVINCE);
        return place;
    }

    public Hl7Entity newResourceGroup(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Hl7Entity entity = new Hl7Entity();
        entity.setCode( ce);
        entity.setDeterminerCode( cs);
/*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            entity.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            entity.setId( set_ii.union(session.getNextII()));
        }
*/
        entity.setClassCode( EntityClass.RGRP);
        return entity;
    }

    public Hl7Entity newGroup(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        Hl7Entity entity = new Hl7Entity();
        entity.setCode( ce);
        entity.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            entity.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            entity.setId( set_ii.union(session.getNextII()));
        }
        */
        entity.setClassCode( EntityClass.GROUP);
        return entity;
    }

    public LanguageCommunication newLanguageCommunication(CE ce, CS cs, SET_II set_ii)
        throws Hl7Exception
    {
        if(cs == null || cs.isNull().isTrue())
            throw validation.newParameterMissingException("determiner");
        LanguageCommunication languagecommunication = new LanguageCommunication();
        languagecommunication.setCode( ce);
        languagecommunication.setDeterminerCode( cs);
        /*        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
        {
            languagecommunication.setId( dataTypeFactory.newSET_II(session.getNextII()));
        } else
        {
            validation.validateOID(set_ii);
            languagecommunication.setId( set_ii.union(session.getNextII()));
        }
        */
        languagecommunication.setClassCode(EntityClass.LCOM);
        return languagecommunication;
    }

    public Hl7Entity makeReference(ReferenceModifier referencemodifier, CS cs, CE ce, CS cs1, SET_II set_ii, int i)
        throws Hl7Exception
    {
        Object obj = null;
        if(referencemodifier == null)
            throw validation.newParameterMissingException("modifier");
        if(set_ii == null || set_ii.isNull().isTrue() || set_ii.isEmpty().isTrue())
            throw validation.newParameterMissingException("id");
        if(referencemodifier.compareTo(ReferenceModifier.CREATE_IF) == 0)
        {
            if(cs == null || cs.isNull().isTrue())
                throw validation.newParameterMissingException("entityClass");
            if(cs1 == null || cs1.isNull().isTrue())
                throw validation.newParameterMissingException("determiner");
        }
        if(cs != null && cs.equal(EntityClass.MIC).isTrue())
        {
            obj = new NonPersonLivingSubject(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.MIC);
        } else
        if(cs != null && cs.equal(EntityClass.CITY).isTrue())
        {
            obj = new Place(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.CITY);
        } else
        if(cs != null && cs.equal(EntityClass.LIV).isTrue())
        {
            obj = new LivingSubject(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.LIV);
        } else
        if(cs != null && cs.equal(EntityClass.ORG).isTrue())
        {
            obj = new Organization(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.ORG);
        } else
        if(cs != null && cs.equal(EntityClass.HCE).isTrue())
        {
            obj = new Hl7Entity(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.HCE);
        } else
        if(cs != null && cs.equal(EntityClass.MODDV).isTrue())
        {
            obj = new Device(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.MODDV);
        } else
        if(cs != null && cs.equal(EntityClass.DEV).isTrue())
        {
            obj = new Device(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.DEV);
        } else
        if(cs != null && cs.equal(EntityClass.COUNTRY).isTrue())
        {
            obj = new Place(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.COUNTRY);
        } else
        if(cs != null && cs.equal(EntityClass.MMAT).isTrue())
        {
            obj = new ManufacturedMaterial(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.MMAT);
        } else
        if(cs != null && cs.equal(EntityClass.CONT).isTrue())
        {
            obj = new Container(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.CONT);
        } else
        if(cs != null && cs.equal(EntityClass.NAT).isTrue())
        {
            obj = new Organization(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.NAT);
        } else
        if(cs != null && cs.equal(EntityClass.FOOD).isTrue())
        {
            obj = new Material(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.FOOD);
        } else
        if(cs != null && cs.equal(EntityClass.PROVINCE).isTrue())
        {
            obj = new Place(referencemodifier);
            ((InfrastructureRoot) (obj)).setClassCode( EntityClass.PROVINCE);
        } else
        if(cs != null && cs.equal(EntityClass.CHEM).isTrue())
        {
            obj = new Material(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.CHEM);
        } else
        if(cs != null && cs.equal(EntityClass.NLIV).isTrue())
        {
            obj = new NonPersonLivingSubject(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.NLIV);
        } else
        if(cs != null && cs.equal(EntityClass.RGRP).isTrue())
        {
            obj = new Hl7Entity(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.RGRP);
        } else
        if(cs != null && cs.equal(EntityClass.PLC).isTrue())
        {
            obj = new Place(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.PLC);
        } else
        if(cs != null && cs.equal(EntityClass.PLNT).isTrue())
        {
            obj = new NonPersonLivingSubject(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.PLNT);
        } else
        if(cs != null && cs.equal(EntityClass.PUB).isTrue())
        {
            obj = new Organization(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.PUB);
        } else
        if(cs != null && cs.equal(EntityClass.COUNTY).isTrue())
        {
            obj = new Place(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.COUNTY);
        } else
        if(cs != null && cs.equal(EntityClass.CER).isTrue())
        {
            obj = new Device(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.CER);
        } else
        if(cs != null && cs.equal(EntityClass.STATE).isTrue())
        {
            obj = new Organization(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.STATE);
        } else
        if(cs != null && cs.equal(EntityClass.HOLD).isTrue())
        {
            obj = new Container(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.HOLD);
        } else
        if(cs != null && cs.equal(EntityClass.PSN).isTrue())
        {
            obj = new Person(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.PSN);
        } else
        if(cs != null && cs.equal(EntityClass.ANM).isTrue())
        {
            obj = new NonPersonLivingSubject(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.ANM);
        } else
        if(cs != null && cs.equal(EntityClass.MAT).isTrue())
        {
            obj = new Material(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.MAT);
        } else
        if(cs != null && cs.equal(EntityClass.GROUP).isTrue())
        {
            obj = new Hl7Entity(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( EntityClass.GROUP);
        } else
        {
            obj = new Hl7Entity(referencemodifier);
            ((Hl7Entity) (obj)).setClassCode( cs);
        }
        ((Hl7Entity) (obj)).setCode( ce);
        ((Hl7Entity) (obj)).setDeterminerCode( cs1);
        return ((Hl7Entity) (obj));
    }

}
